package com.zwg.tingshu.album.api;

import com.zwg.tingshu.album.service.TrackInfoService;
import com.zwg.tingshu.album.service.VodService;
import com.zwg.tingshu.common.login.GuiGuLogin;
import com.zwg.tingshu.common.result.Result;
import com.zwg.tingshu.common.util.AuthContextHolder;
import com.zwg.tingshu.model.album.TrackInfo;
import com.zwg.tingshu.query.album.TrackInfoQuery;
import com.zwg.tingshu.vo.album.AlbumTrackListVo;
import com.zwg.tingshu.vo.album.TrackInfoVo;
import com.zwg.tingshu.vo.album.TrackListVo;
import com.zwg.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;

	/**
	 * 提供给订单服务渲染购买商品（声音）列表-查询当前用户待购买声音列表
	 *
	 * @param trackId 声音ID
	 * @param trackCount 数量
	 * @return
	 */
	@GuiGuLogin
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> getWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable int trackCount) {
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> trackInfoList = trackInfoService.getWaitBuyTrackInfoList(userId, trackId, trackCount);
		return Result.ok(trackInfoList);
	}


	/**
	 * 获取当前用户分集购买声音列表
	 *
	 * @param trackId 声音ID
	 * @return [{name:"本集", price:0.2, trackCount:1},{name:"后10集", price:2, trackCount:10},...,{name:"全集", price:*, trackCount:*}]
	 */
	@GuiGuLogin
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> getUserWaitBuyTrackPayList(@PathVariable Long trackId) {
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> list = trackInfoService.getUserWaitBuyTrackPayList(trackId);
		return Result.ok(list);
	}

	/**
	 * 根据声音ID，获取声音统计信息
	 * @param trackId
	 * @return
	 */
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		return Result.ok(trackInfoService.getTrackStatVo(trackId));
	}

	/**
	 * 用于小程序端专辑页面展示分页声音列表，动态根据用户展示声音付费标识
	 *
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiGuLogin(required = false)
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
		//1.获取用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.封装分页对象
		Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
		//3.调用业务层封装分页对象
		pageInfo = trackInfoService.getAlbumTrackPage(pageInfo, albumId, userId);
		return Result.ok(pageInfo);
	}


	/**
	 * 将音频文件上传到腾讯云点播平台
	 *
	 * @param file 音频文件
	 * @return {mediaFileId:"文件唯一标识",mediaUrl:"播放地址"}
	 */
	@GuiGuLogin
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String, String>> uploadTrack(MultipartFile file) {
		Map<String, String> mapResult = vodService.uploadTrack(file);
		return Result.ok(mapResult);
	}

	/**
	 * 保存专辑下声音
	 *
	 * @param trackInfoVo 声音信息VO对象
	 * @return
	 */
	@GuiGuLogin
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		//1.获取用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.调用业务层新增声音
		trackInfoService.saveTrackInfo(userId, trackInfoVo);
		return Result.ok();
	}


	/**
	 * 获取当前登录声音分页列表
	 *
	 * @param page 页码
	 * @param limit 页大小
	 * @param trackInfoQuery 查询条件
	 * @return
	 */
	@GuiGuLogin
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> getUserTrackByPage(@PathVariable int page, @PathVariable int limit, @RequestBody TrackInfoQuery trackInfoQuery) {
		//1.获取用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.封装分页查询对象
		trackInfoQuery.setUserId(userId);
		//3.调用业务层进行分页
		Page<TrackListVo> PageInfo = new Page<>(page, limit);
		PageInfo = trackInfoService.getUserTrackByPage(PageInfo, trackInfoQuery);
		return Result.ok(PageInfo);
	}

	/**
	 * 根据声音ID查询声音信息
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	/**
	 * 修改声音信息
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@GuiGuLogin
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}

	/**
	 * 根据ID删除声音
	 * @param id
	 * @return
	 */
	@GuiGuLogin
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

}

